﻿using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
using System.Data.SQLite;
using System.Net.Http.Headers;
using SimpleModel.Env;

namespace SimpleModel.DAL
{
    public class SqlBuilder
    {
        enum DBType
        {
            SqlServer2000,
            SqlServer,
            Oracle,
            SQLite,
            MySQL
        }
        
        private DBType dbType;//数据库类型
        private DbProviderFactory dbProvider;//数据库Provider
        private string connectionString;//连接字符串
        private char pSymbol = '@';//参数符号
        private static readonly SqlBuilder builder = null;

        static SqlBuilder()
        {
            builder = new SqlBuilder(DBConfig.connectionConfig);
        }

        public static SqlBuilder GetBuilder()
        {
            return builder;
        }

        private SqlBuilder(string connectionName)
        {
            try
            {
                if (ConfigurationManager.ConnectionStrings.Count > 0)
                {
                    var config = string.IsNullOrEmpty(connectionName)
                        ? ConfigurationManager.ConnectionStrings[0]
                        : ConfigurationManager.ConnectionStrings[connectionName];
                    dbProvider = DbProviderFactories.GetFactory(config.ProviderName);
                    connectionString = config.ConnectionString;
                    CommonConstruct(config.ProviderName);
                }
            }
            catch (Exception ex)
            {
                
            }
            if(dbProvider == null)
            {
                dbProvider = DbProviderFactories.GetFactory(DBConfig.defaultProviderName);
                connectionString = DBConfig.defaultConnectionString;
                CommonConstruct(DBConfig.defaultProviderName);
            }
        }

        private SqlBuilder(DbProviderFactory provider, string connectionString)
        {
            this.dbProvider = provider;
            this.connectionString = connectionString;
            CommonConstruct(provider.GetType().Name);
        }

        private void CommonConstruct(string dbtype = "")
        {
            if (dbtype.StartsWith("System.Data.SqlClient")) dbType = DBType.SqlServer;
            else if (dbtype.IndexOf("Oracle", StringComparison.InvariantCultureIgnoreCase) >= 0) dbType = DBType.Oracle;
            else if (dbtype.IndexOf("SQLite", StringComparison.InvariantCultureIgnoreCase) >= 0) dbType = DBType.SQLite;
            else if (dbtype.IndexOf("MySql", StringComparison.InvariantCultureIgnoreCase) >= 0) dbType = DBType.MySQL;

            if (dbType == DBType.Oracle)
                pSymbol = ':';
            else
                pSymbol = '@';
        }

        public DbConnection CreateConnection()
        {
            var conn = dbProvider.CreateConnection();
            conn.ConnectionString = connectionString;
            return conn;
        }
        
        public DbDataAdapter CreateAdapter(DbCommand selecteCommand)
        {
            var adapter = dbProvider.CreateDataAdapter();
            adapter.SelectCommand = selecteCommand;
            return adapter;
        }

        public DbParameter CreateParameter(string name, object value, DbType t = DbType.Object, ParameterDirection pDirection = ParameterDirection.Input)
        {
            var para = dbProvider.CreateParameter();
            if (t != DbType.Object) para.DbType = t;
            para.Direction = pDirection;
            if (name[0] == pSymbol)
            {
                para.ParameterName = name;
            }
            else
            {
                para.ParameterName = pSymbol + name;
            }
            para.Value = value;
            return para;
        }

    }
}
